#!/bin/bash

[ -f $YK_sh_path ] && rm $YK_sh_path;

YK_attEdition="1.1.0";

YK_getUnixPath () {
  echo $1 | gawk '{print gensub(/\\/, "/", "g")}'
}
YK_getWinPath () {
  echo $1 | gawk '{print gensub(/\//, "\\\\", "g")}'
}
YK_getSlashedWinPath () {
  echo $1 | gawk '{print gensub(/\//, "\\\\\\\\", "g")}'
}

#path without postfix is unix path
#init not-in-workspace path
WD_unix=`YK_getUnixPath $WD`
WS_unix=$WD_unix/../workspace
YK_wsname=$WS_unix/.wsname
YK_hostspath=C:/windows/system32/drivers/etc/hosts
YK_libpath=$WD_unix
YK_lib_jsdoc=$YK_libpath/jsdoc-toolkit-2.1.0
YK_jslint_js=$YK_libpath/jslint.js
YK_rhino_scripts=$YK_libpath'/rhino/script'
YK_yuicompressor=$YK_libpath/yuicompressor-2.4.2.jar
YK_compiler=$YK_libpath/compiler.jar
YK_v8_scripts=$YK_libpath'/v8/scripts'

YK_setWsPath () {
  YK_activews_path=$WS_unix/$1
  YK_activews_svn=$YK_activews_path/svn
  YK_activews_temp=$YK_activews_path/temp
  YK_activews_export=$YK_activews_temp/export
  YK_activews_pkgs=$YK_activews_temp/pkgs
  YK_activews_jslint=$YK_activews_temp/jslint
  YK_activews_jsdoc=$YK_activews_path/jsdoc
  YK_wsconf=$YK_activews_path/.wsconf
}

if [ -f "$YK_wsname" ]; then
  YK_activews=`cat $YK_wsname | gawk -F":" '/^workspace:/{print $2}'`;
  YK_setWsPath $YK_activews
fi

YK_getParam () {
  if [ ! -e $YK_wsconf ]; then 
    touch $YK_wsconf
  fi
  cat $YK_wsconf | gawk -F":" '/^'$1':/{match($0,/^'$1':([^$]+)$/,aa);print aa[1]}';
}
YK_setParam () {
  if [ ! -e $YK_wsconf ]; then 
    touch $YK_wsconf
  fi
  if [ `cat $YK_wsconf | gawk '/^'$1':/{print "exists"}'` ];then 
    sed -i '/^'$1':[^$]*$/c\\'$1':'"$2" $YK_wsconf
  else
    echo $1:$2 >> $YK_wsconf
  fi;
}
YK_getUntilSet () {
  #$1:attr name, $2:var for the attr value, $3:attr description, $4:default value, $5:read option
  local tvalue=`YK_getParam $1`
  if [ "$tvalue" == "" ]; then
    if [ "$4" == "" ]; then
      while [ "$tvalue" == "" ]; do
        echo -n "Please enter $3:"
        read $5 tvalue
      done
    else
      echo -n "Please enter $3[default $4]:"
      read $5 tvalue
      if [ "$tvalue" == "" ]; then
        tvalue="$4"
      fi
    fi
    YK_setParam $1 "$tvalue"
  fi
  tvalue=`echo "'$tvalue'" | gawk '{print gensub(/\\\\/, "\\\\\\\\\\\\\\\\", "g")}'`
  tvalue=`echo "'$tvalue'" | gawk '{print gensub(/ /, "\\\\\\\\ ", "g")}'`
  eval $2="$tvalue"
}

YK_changeHosts () {
  local ii
  local hn
  local hip
  local temp
  for ii in `echo $*| gawk -F"," '{for(i=1;i<=NF;i++) print gensub(/ /,",",1,$i)}'`;do
    hn=`echo $ii | gawk -F"," '{print $2}'`
    hip=`echo $ii | gawk -F"," '{print $1}'`
    temp=`cat $YK_hostspath | sed 's/[^0-9]*\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)\.* \('$hn'\)[ \t]*/#\1 \2/' | grep -v '#'$hip' '$hn`
    if [ "$hip" == '#' ];then
      echo -e "$temp" > $YK_hostspath
      echo "$hn host record is commented."
    else
      echo -e "$temp\n$hip $hn" > $YK_hostspath
      echo "$hn's ip changed to $hip"
    fi
  done
}

YK_openDir () {
  local dirSetting=`YK_getParam dirbrowser`
  if [ "$dirSetting" == "" ];then
    explorer `YK_getWinPath $1` &
  elif [ "$dirSetting" == "vim" ];then
    vim $1
  else
    eval "$dirSetting `YK_getSlashedWinPath $1` &"
  fi
}

YK_isEnc () {
  local temp=`iconv -f $2 -t gbk $1 1>/dev/null 2>/dev/null && echo 'true'`;
  if [ "$temp" = 'true' ]; then
    return 0;
  fi;
  return -1;
}

YK_doTarget () {
  case "$1" in
  "so")
    YK_getUntilSet svnuri YK_svnuri 'SVN URI'
    YK_getUntilSet svnname YK_svnname 'SVN user name'
    svn co $YK_svnuri $YK_activews_svn --username=$YK_svnname
    ;;
  "su")
    (cd $YK_activews_svn && svn up)
    ;;
  "ss")
    (cd $YK_activews_svn && svn st)
    ;;
  "sa")
    local sapath
    (cd $YK_activews_svn && svn st)
    echo "Please enter the file you want to add(use format listed above)"
    read sapath
    while [ ! -e $YK_activews_svn/$sapath ];do
      echo -n "File NOT exists, please reenter:"
      read sapath
    done
    (cd $YK_activews_svn && svn add $YK_activews_svn/$sapath)
    ;;
  "sr")
    local srpath
    (cd $YK_activews_svn && ls -R)
    echo "Please enter the file you want to remove(using format listed above)"
    read srpath
    while [ ! -e $YK_activews_svn/$srpath ];do
      echo -n "File NOT exists, please reenter:"
      read srpath
    done
    (cd $YK_activews_svn && svn rm $YK_activews_svn/$srpath)
    ;;
  "sc")
    (cd $YK_activews_svn && svn st)
    local ii
    local svncomment
    for ii in `(cd $YK_activews_svn && svn st | gawk '{print "'"$YK_activews_svn/"'"$2}')`;do
      YK_doTarget _make_jslint $ii
    done
    echo -n "Please enter your comments(or directly press enter to cancel commit):"
    read svncomment
    if [ "$svncomment" ]; then
      (cd $YK_activews_svn && svn ci -m "$svncomment")
    else
      echo "svn ci canceled"
    fi
    ;;
  "wo")
    YK_openDir $YK_activews_svn
    ;;
  "op")
    YK_openDir $YK_activews_pkgs
    ;;
  "wn")
    echo -n "Please enter the name of your workspace:";
    read YK_activews;
    while [ "$YK_activews" == "" ]; do
      echo -n "The name can't be empty, please reenter:";
      read YK_activews;
    done
    YK_setWsPath $YK_activews
    mkdir -p $YK_activews_path;

    cp $YK_lib_jsdoc/conf/jsdoc.conf $YK_activews_path
    local iscopy
    echo "Copy .wsconf file from other workspace?[y/n]";
    read iscopy
    if [ "$iscopy" == "y" ];then
      echo "All workspaces are:" && ls $WS_unix;
      echo -n "Please enter the name of workspace your want to copy .wsconf from:";
      read iscopy
      while [ ! -f $WS_unix/$iscopy/.wsconf ]; do
        echo -n ".wsconf NOT exists, please reenter:";
        read iscopy
      done
      cp $WS_unix/$iscopy/.wsconf $YK_activews_path
    fi

    echo "Your workspace is: $YK_activews_path";
    echo "workspace:$YK_activews" > $YK_wsname;
    ;;
  "ws")
    echo "All workspaces :" && ls $WS_unix;
    echo -n "Please enter the workspace you want switch to:";
    #an never exists dir
    read YK_homedirname;
    [ "$YK_homedirname" == "" ] && YK_homedirname="0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0_=!@#%^**"
    while [ ! -d $WS_unix/$YK_homedirname ]; do
      echo -n "Workspace NOT exist, please reenter:";
      read YK_homedirname;
      if [ "$YK_homedirname" == "" ]; then
        YK_homedirname="0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0_=!@#%^**"
      fi
    done
    YK_activews=$YK_homedirname
    YK_setWsPath $YK_activews
    echo "Workspace is switched to $YK_activews_path";
    echo "workspace:$YK_activews" > $YK_wsname;
    ;;
  "c")
    clear
    ;;
  "yc")
    local isUtf8=`YK_isEnc $2 utf-8 && echo 'true'`
    local fCharSet=utf8
    local outPath=$3
    if [ "$isUtf8" != "true" ]; then
      fCharSet=gbk;
    fi;
    if [ "$outPath" == "" ]; then
      outPath=${2%.*}_min.${2##*.}
    fi;
    if [ ${2##*.} == 'js' ]; then
      YK_doTarget rn hexencode $2 $outPath'.tmp' $fCharSet
    fi
    echo java -jar ${YK_yuicompressor} --charset ${fCharSet} -o $outPath $outPath'.tmp';
    java -jar ${YK_yuicompressor} --charset ${fCharSet} -o $outPath $outPath'.tmp';
    #rm $outPath'.tmp'
    ;;
  "gc")
    local isUtf8=`YK_isEnc $2 utf-8 && echo 'true'`
    local fCharSet=utf8
    local outPath=$3
    if [ "$isUtf8" != "true" ]; then
      fCharSet=gbk;
    fi;
    if [ "$outPath" == "" ]; then
      outPath=${2%.*}_min.${2##*.}
    fi;
    YK_doTarget rn hexencode $2 $outPath'.tmp.js' $fCharSet
    #echo java -jar ${YK_compiler} --compilation_level SIMPLE_OPTIMIZATIONS --js_output_file $outPath --js $outPath'.tmp.js';
    java -jar ${YK_compiler} --compilation_level SIMPLE_OPTIMIZATIONS --js_output_file $outPath --js $outPath'.tmp.js';
    rm $outPath'.tmp.js'
    ;;
  "wc")
    if [ "$2" = "" ]; then
      echo -n 'Enter the filter keyword:'
      read YK_cfile
    else
      YK_cfile=$2
    fi
    if [ `echo $YK_cfile | gawk -F'|' '{print NF}'` -gt 1 ]; then
      for ii in `echo $YK_cfile | gawk -F'|' '{for(i=1;i<=NF;i++) print $i; }'`; do
        YK_doTarget wc $ii
      done
      YK_needCr='0'
      return
    fi
    YK_doTarget _make_export $YK_cfile
    YK_doTarget _make_clean_pkgs 
    YK_getUntilSet svnuri YK_svnuri 'SVN URI'
    YK_getUntilSet svnname YK_svnname 'SVN User name'
    YK_getUntilSet cdnurl YK_cdnurl 'CDN http server root uri' http://k.kbcdn.com
    local svn_revision=`svn info $YK_svnuri --username=$YK_svnname | gawk '/^Revision: /{print $2}'`;
    local ii
    local iiversion 
    local iiname
    local iitype
    local pkgpath
    local tempfile
    local pkgfile
    local noticestr=""
    local cpnoticestr=""
    local fcount=0
    local cpcount=0
    for ii in `find ${YK_activews_export} -mindepth 1 -type f -path "*${YK_cfile}*"`; do
      iitype=${ii##*.};
      #compress *.js, *.css
      iiname=${ii%.*};
      pkgpath=`echo ${iiname%/*} | sed 's/'$YK_activews'\/temp\/export/'$YK_activews'\/temp\/pkgs/'`
      if [ `echo $iitype | gawk '{if (match($0, /css|js/)){print 1} else {print 2}}'` == '1' ]; then
        fcount=`echo $fcount+1 | bc`
        #YK_doTarget _make_jslint $ii
        iiversion=`gawk -F":" '/^ \* @version:/{sub("\r$", "");print $2}' $ii`;
        if [ ! $iiversion == "" ];then
          iiversion=_$iiversion
        fi
        tempfile=$pkgpath/${iiname##*/}${iiversion}.tmp.${iitype}
        pkgfile=$pkgpath/${iiname##*/}${iiversion}.${iitype}
        mkdir -p $pkgpath
        sed '/^ \* @revision:$/c\ * @revision '$svn_revision $ii > $tempfile;
        sed 's/^ \* @version:/ \* @version /' $tempfile > $pkgfile;
        local isUtf8=`YK_isEnc $pkgfile utf-8 && echo 'true'`
        local fCharSet=utf8
        if [ "$isUtf8" != "true" ]; then
          fCharSet=gbk;
        fi;
        if [ $iitype == 'js' ]; then
          YK_doTarget rn hexencode $pkgfile $tempfile $fCharSet
        else
          cp $pkgfile $tempfile
        fi
        rm $pkgfile;
        java -jar ${YK_yuicompressor} --charset ${fCharSet} -o $pkgfile $tempfile;
   #     sed '/^ \* revision:$/c\ * revision:'$svn_revision $tempfile > $pkgfile;
        cp $pkgfile E:/out
        rm $tempfile
        noticestr="$noticestr$pkgfile processed.\n"
      elif [ `echo $iitype | gawk '{if (match($0, /jpeg|jpg|gif|png|jpe/)){print 1} else {print 2}}'` == '1' ]; then
        cpcount=`echo $cpcount+1 | bc`
        mkdir -p $pkgpath
        cp $ii $pkgpath
        cpnoticestr="$cpnoticestr${ii##*/} copied.\n"
      elif [ `echo $iitype | gawk '{if (match($0, /html|htm/)){print 1} else {print 2}}'` == '1' ]; then
        fcount=`echo $fcount+1 | bc`
        mkdir -p $pkgpath
        cp $ii $pkgpath
        pkgfile=$pkgpath/${ii##*/}
        for il in `cat $ii | grep "<link" | grep 'href="\.' | sed 's/.*href="\([^"]*\)".*/\1/'`;do
          #local newpath=`YK_getAbsFromRel $il $`
          local fsvnpath=`echo $pkgpath | sed 's/temp\/pkgs/svn/'`
          local lfsvnpath=`(cd $fsvnpath && cd ${il%/*} && pwd)`
          local lfname=${il##*/}
          iiversion=`gawk -F":" '/^ \* @version:/{sub("\r$", "");print $2}' $lfsvnpath/$lfname`;
          if [ ! $iiversion == "" ];then
            iiversion=_$iiversion
          fi
          local newCdnPath=$YK_cdnurl${lfsvnpath#"`(cd $YK_activews_svn && pwd)`"}/${lfname%.*}$iiversion.${lfname##*.}
          newCdnPath=`echo $newCdnPath | gawk '{print gensub(/\//, "\\\\\\\\/", "g")}'`
          #pkgpath=`echo ${iiname%/*} | sed 's/'$YK_activews'\/temp\/export/'$YK_activews'\/temp\/pkgs/'`
          sed 's/\(.*<link .*href="\)[^"]*'$lfname'[^"]*\(".*\)/\1'$newCdnPath'\2/g' $ii > $pkgfile
          cp $pkgfile $ii
        done
        noticestr="$noticestr$pkgfile processed.\n"
      fi
    done;
    echo -e "\n\n========== Processed files list =========="
    echo -e $noticestr
    echo -e "\n========== Copied files list =========="
    echo -e $cpnoticestr
    echo "$fcount file(s) processed in $YK_activews_pkgs"
    echo "$cpcount file(s) copied in $YK_activews_pkgs"
    echo "Use target op) to open pkgs directory"
    ;;
  "hd")
    YK_getUntilSet hostdevelop YK_hostdevelop 'development host conf(seperate by ,)'
    YK_changeHosts $YK_hostdevelop
    ;;
  "ht")
    YK_getUntilSet hosttest YK_hosttest 'test host conf(seperate by ,)'
    YK_changeHosts $YK_hosttest
    ;;
  "hp")
    YK_getUntilSet hostprod YK_hostprod 'production host conf(seperate by ,)'
    YK_changeHosts $YK_hostprod
    ;;
  "ut")
    #$2 is provided if as filter word to auto upload from pkgs
    YK_getUntilSet testip YK_testip 'test server ip' 10.5.58.131
    YK_getUntilSet testname YK_testname 'test server username' uitest
    YK_getUntilSet testpwd YK_testpwd 'test server password' uitest '-s'
    YK_getUntilSet testpath YK_testpath 'test server http root path' /images/k.kbcdn.com
    YK_getUntilSet testurl YK_testurl 'test http root uri' http://k.kbcdn.com
    local useSvnOrPkg
    if [ "$2" = "" ];then
      echo -n "upload from svn(s) or pkgs(p) dir?[s/p]"
      read useSvnOrPkg
      if [ "$useSvnOrPkg" == "s" ];then
        useSvnOrPkg=$YK_activews_svn
      else
        useSvnOrPkg=$YK_activews_pkgs
      fi
      ls -R $useSvnOrPkg
      echo -n 'Please enter the name:'
      read YK_cfile
    else
      useSvnOrPkg=$YK_activews_pkgs
      YK_cfile=$2
    fi
    local ii
    local rpath
    local rpathfile
    local pedfiles=""
    local tfcount=0
    local haha
    for ii in `find ${useSvnOrPkg} -mindepth 1 -type f -path "*${YK_cfile}*" | grep -v "\.svn"`; do

      #ignore /xxx/xxx/.xxx.xx.x
      if [ `echo ${ii##*/} | gawk '{print substr($0,1,1)}'` == "." ];then
        continue;
      fi

      echo Do you want upload
      echo $ii
      echo -n to the test server[y/n]?
      read rpath
      if [ "$rpath" == "y" ]; then
        rpathfile=${ii##"$useSvnOrPkg"};
        rpath=${rpathfile%/*}
        rpathfile=${ii##*/};
        haha=`lftp -u $YK_testname,$YK_testpwd -e 'mkdir -p '$YK_testpath$rpath';cd '$YK_testpath$rpath';lcd '$useSvnOrPkg$rpath';put '$rpathfile';bye;' sftp://$YK_testip 2>&1`
        haha=`echo $haha | sed 's/.*[^0-9]\([0-9]\+\) bytes transferred/\1/' | sed '/[^0-9]/c\ '`
        if [ "$haha" == "" ];then
          haha="0"
        fi
        if [ "$haha" -gt "0" ];then
          echo "transported $haha bytes, upload success!"
          pedfiles=$pedfiles"\n$YK_testurl$rpath/$rpathfile" 
          tfcount=`echo $tfcount+1 | bc`
        else
          echo $YK_testurl$rpath/$rpathfile upload FAILED! please check!
        fi
      fi
    done
    echo "$tfcount files uploaded, change hosts to test conf and check the uri below:"
    echo -e $pedfiles
    ;;
  "uc")
    echo -n "files in CDN server can't be deleted or changed! Please enter \"cdn\" to confirm:"
    read YK_cfile
    if [ "$YK_cfile" == "cdn" ]; then
      YK_getUntilSet cdnip YK_cdnip 'CDN server ip' 10.35.84.112
      YK_getUntilSet cdnname YK_cdnname 'CDN user name' ui
      YK_getUntilSet cdnpath YK_cdnpath 'CDN server http root path' /tux
      YK_getUntilSet cdnurl YK_cdnurl 'CDN server http root uri' http://k.kbcdn.com

      YK_cdnpwd=`YK_getParam cdnpwd`
      if [ "$YK_cdnpwd" != "" ];then
        YK_cdnpwd=','$YK_cdnpwd
      fi

      ls -R $YK_activews_pkgs
      echo -n 'files in pkgs are listed above, please enter the filter keyword:'
      read YK_cfile
      local ii
      local rpath
      local rpathfile
      local hasfile
      local pedfiles=""
      local tfcount=0
      local haha
      for ii in `find ${YK_activews_pkgs} -mindepth 1 -type f -path "*${YK_cfile}*"`; do
        echo 'Do you want to upload'
        echo $ii
        echo -n 'to the CDN[y/n]?'
        read rpath
        if [ "$rpath" == "y" ]; then
          rpathfile=${ii##"$YK_activews_pkgs"};
          rpath=${rpathfile%/*}
          rpathfile=${ii##*/};
          hasfile=`lftp -u $YK_cdnname$YK_cdnpwd -e "ls $YK_cdnpath$rpath;bye" sftp://$YK_cdnip| grep $rpathfile`;
          if [ "$hasfile" ]; then
            echo File $rpathfile exists on CDN, cancel uploading
          else
            haha=`lftp -u $YK_cdnname$YK_cdnpwd -e 'mkdir -p '$YK_cdnpath$rpath';cd '$YK_cdnpath$rpath';lcd '$YK_activews_pkgs$rpath';put '$rpathfile';bye;' sftp://$YK_cdnip 2>&1`
            haha=`echo $haha | sed 's/.*[^0-9]\([0-9]\+\) bytes transferred/\1/' | sed '/[^0-9]/c\ '`
            if [ "$haha" == "" ];then
              haha="0"
            fi
            if [ "$haha" -gt "0" ];then
              echo "$haha bytes transported, upload success!"
              pedfiles=$pedfiles"\n$YK_cdnurl$rpath/$rpathfile" 
              tfcount=`echo $tfcount+1 | bc`
            else
              echo $YK_cdnurl$rpath/$rpathfile upload failed! please check.
            fi
          fi
        fi
      done
      echo "$tfcount file(s) uploaded to the CDN server, change hosts config to CDN and access following url(s):"
      echo -e $pedfiles
    fi
    ;;
  "jc")
    ls -R $YK_activews_svn
    echo -n 'files in local svn are listed above, please enter the filter keyword:'
    read YK_cfile
    local ii
    for ii in `find ${YK_activews_svn} -mindepth 1 -type f -path "*${YK_cfile}*" | grep -v "/.svn/"`; do
      echo Do you want to check
      echo $ii
      echo -n "?[y/n]"
      read YK_cfile
        if [ "$YK_cfile" == "y" ]; then
          YK_doTarget _make_jslint $ii
        fi
    done
    ;;
  "ew")
    YK_getUntilSet editor YK_editor 'Custom editor path:' vim
    if [ "$YK_editor" == "vim" ]; then
      vim $YK_wsconf
    else
      eval "$YK_editor $YK_wsconf &"
    fi
    ;;
  "eh")
    YK_getUntilSet editor YK_editor 'editor path:' vim
    if [ "$YK_editor" == "vim" ]; then
      vim $YK_hostspath
    else
      eval "$YK_editor $YK_hostspath &"
    fi
    ;;
  "as")
    YK_getUntilSet apachestart YK_apache_start 'command to start apache server'
    (cd ${YK_apache_start%/*} && $YK_apache_start &)
    ;;
  "at")
    YK_getUntilSet apachestop YK_apache_stop 'command to stop apache server'
    (cd ${YK_apache_stop%/*} && $YK_apache_stop &)
    ;;
  "jd")
    #YK_getUntilSet apachestop YK_apache_stop 'command to stop apache server'
    #java -jar jsrun.jar app/run.js -a -t=templates/jsdoc -r=3 ../../workspace/koubei/svn/global/*/*.js
    ;;
  "mj")
    YK_doTarget _make_export $2;
    #YK_getUntilSet apachestop YK_apache_stop 'command to stop apache server'
    local ii
    local aJsFile
    for ii in `find ${YK_activews_export} -mindepth 1 -type f -path "*.js"`; do
      aJsFile=$aJsFile' '$ii;
    done;
    (cd $YK_lib_jsdoc && java -jar jsrun.jar app/run.js -c=$YK_activews_path/jsdoc.conf -d=$YK_activews_jsdoc $aJsFile)
    echo 'jsdoc is created at: '$YK_activews_jsdoc
    ;;
  "uj")
    lftp -u 'root,123456' -e 'cd /var/www/jsdoc;lcd '$YK_activews_jsdoc';mirror -R;bye;' sftp://kxt.koubei.com
    #YK_getUntilSet apachestop YK_apache_stop 'command to stop apache server'
    ;;
  "cp1")
    local filterPhrase
    local ii
    local processList=()
    local len
    local nn
    echo -n 'Please enter filter keywords[like:"jsbase/*.js"]:'
    read filterPhrase
    for ii in `find ${YK_activews_svn} -mindepth 1 -type f -path "*${filterPhrase}*" | grep -v "\.svn"`; do
      if [ `echo ${ii##*/} | gawk '{print substr($0,1,1)}'` == "." ];then
        continue;
      fi
      processList["${#processList[@]}"]=${ii#"$YK_activews_svn"}
#       echo -n "process $ii ?[y/n]"
#       read filterPhrase
#       if [ "$filterPhrase" = 'y' ]; then
#         processList["${#processList[@]}"]=$ii
#       fi
    done
    len=${#processList[@]}
    ii=0;
    while [ $ii -lt $len ];do
      nn=${processList["$ii"]};
      (cd $YK_activews_svn && svn ci ./$nn -m '')
      let ii++;
    done
    YK_doTarget wc $filterPhrase
    YK_doTarget ut $filterPhrase
    ;;
  "_make_clean_export")
    rm -rf $YK_activews_export
    ;;
  "_make_export")
    if [ -e $YK_activews_export ];then
      YK_doTarget _make_clean_export
    fi
    local ii;
    local lvl1name="";
    local lvl2name="";
    local lvl3name="";
    local same2="1";
    local same3="1";
    local tempn;
    if [ "$2" != "" ]; then
      for ii in `find ${YK_activews_svn} -mindepth 1 -type f -path "*${2}*" | grep -v "\.svn"`; do
        if [ `echo ${ii##*/} | gawk '{print substr($0,1,1)}'` == "." ];then
          continue;
        fi
        ii=${ii##*workspace/"${YK_activews}"/svn/}
        tempn=${ii%%/*} 
        if [ "$lvl1name" = "" ]; then
          lvl1name=$tempn
        elif [ "$lvl1name" != "$tempn" ]; then
          lvl1name=''
          lvl2name=''
          lvl3name=''
          break;
        fi;
        if [ "$same2" = "1" ]; then
          tempn=${ii#*/}
          if [ $tempn != ${tempn%%/*} ];then
            tempn=${tempn%%/*}
            if [ "$lvl2name" = "" ]; then
              lvl2name=/$tempn
            elif [ "$lvl2name" != "/$tempn" ]; then
              lvl2name='';
              lvl3name='';
              same2=2;
              same3=2;
            fi;
          else
            lvl2name='';
            lvl3name='';
            same2=2;
            same3=2;
          fi;
        fi;
        if [ "$same3" = "1" ]; then
          tempn=${ii#*/*/}
          if [ $tempn != ${tempn%%/*} ];then
            tempn=${tempn%%/*}
            if [ "$lvl3name" = "" ]; then
              lvl3name=/$tempn
            elif [ "$lvl3name" != "/$tempn" ]; then
              lvl3name='';
              same3=2;
            fi;
          else
            lvl3name='';
            same3=2;
          fi;
        fi;
      done;
      lvl1name=/$lvl1name$lvl2name$lvl3name
    else
      lvl1name=''
    fi
    #mkdir -p $YK_activews_temp
    mkdir -p $YK_activews_export$lvl1name
    YK_getUntilSet svnuri YK_svnuri 'SVN uri'
    YK_getUntilSet svnname YK_svnname 'SVN username'
    svn export $YK_svnuri$lvl1name $YK_activews_export$lvl1name --username=$YK_svnname --force
    ;;
  "_make_clean_pkgs")
    if [ -e $YK_activews_pkgs ];then
      rm -rf $YK_activews_pkgs
    fi
    ;;
  "_make_jslint")
    if [ `echo ${2##*.} | gawk '{if (match($0, /css|js|html/)){print 1} else {print 2}}'` == '1' ]; then
      echo "jsLint starts to check: $2"
      mkdir -p $YK_activews_jslint
      rm -rf $YK_activews_jslint/*
      local isUtf8=`YK_isEnc $2 utf-8 && echo 'true'`
      local tempfile=$YK_activews_jslint/temp.${2##*.}
      if [ "$isUtf8" = "true" ]; then
        #echo 'is utf8';
        iconv -t GBK -f UTF-8 $2 > $tempfile
      else
        #echo 'is gbk';
        cat $2 > $tempfile
      fi
      java -cp $YK_libpath/rhino/js.jar org.mozilla.javascript.tools.shell.Main $YK_jslint_js $tempfile
      rm -f $tempfile
      echo "jsLint finished checking $2"
    else
      echo 'File is css, js or html type, cancel jsLint check'
    fi
    ;;
  "make_get_asc")
    (cd $WD && gpg -ea -r 98C4461C -o ../asc/config.asc --yes ../workspace/os/svn/trunk/other/k-target.sh 2>/dev/null)
    echo done;
    YK_action='0';
    ;;
  "make_tag_du")
    echo -n 'Enter Tag:'
    local tagName
    read tagName
    YK_doTarget make_du
    mkdir -p $YK_activews_svn/tags/$tagName/pkgs
    mkdir -p $YK_activews_svn/tags/$tagName/src
    cp -r $YK_activews_pkgs/trunk/src/* $YK_activews_svn/tags/$tagName/pkgs/
    cp -r $YK_activews_export/trunk/src/* $YK_activews_svn/tags/$tagName/src/
    (cd $YK_activews_svn && svn add tags/$tagName && svn ci tags/$tagName -m 'add tag "'$tagName'" pkgs code')
    ;;
  "make_du")
    local duDir=$YK_activews_pkgs/trunk/src/du
    local duJs=$duDir/du.js
    YK_doTarget wc '/trunk/src'
    mkdir -p $duDir
    cat $YK_activews_svn/trunk/src/du/top_comment > $duJs
    cat $YK_activews_pkgs/trunk/src/core/core.js >> $duJs
    cat $YK_activews_pkgs/trunk/src/tool/tool.js >> $duJs
    cat $YK_activews_pkgs/trunk/src/json/json.js >> $duJs
    cat $YK_activews_pkgs/trunk/src/anim/anim.js >> $duJs
    cat $YK_activews_pkgs/trunk/src/popup/popup.js >> $duJs
    ;;
  "make_st")
    local stDir=$YK_activews_svn'/../../os/svn/trunk/other/stauren.net'
    local stMisc="C:/Users/stauren/Desktop/My Dropbox/code/stauren.org/misc"
    #java -jar ${YK_yuicompressor} --charset utf8 -o "$stMisc/js/site_min.js" $stDir/site.js
    java -jar ${YK_compiler} --compilation_level SIMPLE_OPTIMIZATIONS --js_output_file "$stMisc/js/site_min.js" --js $stDir/site.js
    java -jar ${YK_yuicompressor} --charset utf8 -o "$stMisc/css/base.css" "$stMisc/css/base_comment.css"
    java -jar ${YK_yuicompressor} --charset utf8 -o "$stMisc/css/stauren.css" "$stMisc/css/stauren_comment.css"
    cat "$stMisc/css/base.css" > "$stMisc/css/base-stauren.css"
    cat "$stMisc/css/stauren.css" | sed 's/@media all and(/@media all and (/g' >> "$stMisc/css/base-stauren.css"
    ;;
  "make_ds")
    YK_doTarget make_du
    YK_doTarget make_st
    ;;
  "rn")
    if [ -e $YK_rhino_scripts'/'$2'.js' ];then
      YK_needCr='0';
      (cd $YK_libpath/rhino && ./shell.bat $YK_rhino_scripts'/'$2'.js' $YK_rhino_scripts $*)
    fi;
    ;;
  "rl")
    for ii in `ls $YK_rhino_scripts/*.js`;do
      ii=${ii##*/}
      echo -n ${ii%%.*}' '
    done;
    YK_needCr='0';
    echo
    ;;
  "vn")
    if [ -e $YK_v8_scripts'/'$2'.js' ];then
      YK_needCr='0';
      (cd $YK_libpath/v8 && echo 'var _arg="'$YK_v8_scripts'/'$2'.js '$YK_v8_scripts' '$*'"'> ./arg.js && ./shell.bat ./arg.js $YK_v8_scripts'/'$2'.js')
      (cd $YK_libpath/v8 && rm ./arg.js)
    fi;
    ;;
  "vl")
    for ii in `ls $YK_v8_scripts/*.js`;do
      ii=${ii##*/}
      echo -n ${ii%%.*}' '
    done;
    YK_needCr='0';
    echo
    ;;
  "gr")
    YK_getUntilSet staurenip YK_staurenip staurenip
    local stRoot='C:/Users/stauren/Desktop/My Dropbox/code/stauren.org'
    (cd "$stRoot" && lftp -u stauren6437 -e 'mirror httpdocs/runtime/blogdata ./runtime/blogdata;mirror --only-missing httpdocs/uploads ./uploads;bye;' ftp://$YK_staurenip)
    ;;
  "us")
    YK_getUntilSet staurenip YK_staurenip staurenip
    YK_getUntilSet staurenpwd YK_staurenpwd staurenpassword
    local stRoot=$WD/../../stauren.org
    (cd $stRoot && lftp -u$YK_staurenpwd -e 'put ./misc/css/base-stauren.css -o ./httpdocs/misc/css/base-stauren.css;put ./index.php -o ./httpdocs/index.php;put ./lib/stauren/stauren.class.php -o ./httpdocs/lib/stauren/stauren.class.php;put ./lib/stauren/config/stauren_net_config.php -o ./httpdocs/lib/stauren/config/stauren_net_config.php;mirror -R ./lib/stauren/modules httpdocs/lib/stauren/modules;mirror -R ./lib/stauren/templates httpdocs/lib/stauren/templates;mirror -R ./lib/stauren/includes httpdocs/lib/stauren/includes;chmod 0755 ./httpdocs/index.php;chmod -R 0755 ./httpdocs/lib/stauren;bye;' ftp://$YK_staurenip)
    ;;
  "ls")
    echo available commands:
    echo -e '\E[38;5;196mwn) create new workspace (workspace new)'
    echo -e '\E[38;5;202mws) switch workspace (workspace switch)'
    echo -e '\E[38;5;208mwo) open svn dir(workspace open)'
    echo -e '\E[38;5;211myc) compress file by YUI[yc file.js file.out.js] (yui compress)'
    echo -e '\E[38;5;211mgc) compile a js by Google [gc file.js file.out.js] (google compress)'
    echo -e '\E[38;5;214mwc) compress svn js css files (directory compress)'
    echo -e '\E[38;5;220mop) open pkgs dir'
    echo -e '\E[38;5;226mjc) check file using jsLint (jslint check)'
    echo -e '\E[38;5;155mso) svn checkout (svn co)'
    echo -e '\E[38;5;118msu) svn update  (svn up)'
    echo -e '\E[38;5;82mss) svn status (svn st)'
    echo -e '\E[38;5;83msa) svn add (svn add)'
    echo -e '\E[38;5;84msr) svn remove (svn rm)'
    echo -e '\E[38;5;85msc) svn commit  (svn ci)'
    echo -e '\E[38;5;86mhd) change host to develop (hosts develop)'
    echo -e '\E[38;5;87mht) change host to test(hosts test)'
    echo -e '\E[38;5;81mhp) change host to product (hosts product)'
    echo -e '\E[38;5;75mut) upload file to test server(upload test)'
    echo -e '\E[38;5;69muc) upload file to cdn (upload cdn)'
    echo -e '\E[38;5;63mas) start apache server(apache start)'
    echo -e '\E[38;5;93mat) stop apache server(apache terminate)'
    echo -e '\E[38;5;129mew) open .wsconf file (edit wsconf)'
    echo -e '\E[38;5;165meh) open hosts file (edit hosts)'
    echo -e '\E[38;5;201mcp1) combined process1:svn ci,compress,upload (combined process)'
    echo -e '\E[38;5;200mmj) make jsdoc(make jsdoc)'
    echo -e '\E[38;5;199muj) upload jsdoc to server(upload jsdoc)'
    echo -e '\E[38;5;81mmake_get_asc) '
    echo -e '\E[38;5;81mmake_du) '
    echo -e '\E[38;5;81mmake_tag_du) '
    echo -e '\E[38;5;81mmake_st) '
    echo -e '\E[38;5;81mmake_ds) '
    echo -e '\E[38;5;81m_make_jslint) '
    echo -e '\E[38;5;81m_make_clean_export) '
    echo -e '\E[38;5;81m_make_export) '
    echo -e '\E[38;5;81m_make_clean_pkgs) '
    echo -e '\E[38;5;81mls) list all tasks'
    echo -e '\E[38;5;81mrl) list all rhino script'
    echo -e '\E[38;5;81mrn) run rhino script(rn scriptname arguments)'
    echo -e '\E[38;5;81mgr) get runtime data(and uploads dir)'
    echo -e '\E[38;5;81mus) upload stauren(upload st lib to prod)'
    echo -e '\E[38;5;198mc) clear terminal'
    echo -e '\E[38;5;196m0) quit'
    #echo -e '\E[38;5;m'
    echo -en '\E[38;5;46m'
    YK_needCr='0';
    ;;
  *)
    echo action $YK_action not found
    return 1
    ;;
  esac
  return 0;
}

getUserAction () {
  if [ "$YK_activews" == "" ]; then
    cat <<_EOFA
[no workspace is active, you could]:
wn) create new workspace (workspace new)
ws) switch active workspace (workspace switch)
_EOFA
  read YK_action
  else
    echo -e '[active workspace is \E[38;5;231m'$YK_activews' \E[38;5;46m, you could]:'
    echo -e '\E[38;5;81mls) list all tasks'
    echo -e '\E[38;5;81mrl) list all rhino scripts'
    echo -e '\E[38;5;81mrn) run rhino script(rn scriptname arguments)'
    echo -e '\E[38;5;198mc) clear terminal'
    echo -e '\E[38;5;196m0) quit'
    #echo -e '\E[38;5;m'
    echo -en '\E[38;5;46m'
  echo -n [enter task name]:
  read YK_action;
  fi
}

YK_att () {
  echo -e "-----Auto task tool v$YK_attEdition -----";

  YK_action=a100
  while [ "$YK_action" != "0" ]; do
    YK_needCr='1'
    getUserAction
    if [ "$YK_action" != "0" ]; then
      YK_doTarget $YK_action
    fi;
    if [ "$YK_action" != "0" ]; then
      if [ "$YK_needCr" != "1" ]; then
        echo press Enter to continue
        read YK_empty
      fi
    fi
  done;
}
YK_att
exit;
